package com.jhsw.roadsun.module.business.controller.admin.config;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.jhsw.roadsun.framework.common.pojo.PageParam;
import com.jhsw.roadsun.framework.common.pojo.PageResult;
import com.jhsw.roadsun.framework.common.pojo.CommonResult;
import com.jhsw.roadsun.framework.common.util.object.BeanUtils;
import static com.jhsw.roadsun.framework.common.pojo.CommonResult.success;

import com.jhsw.roadsun.framework.excel.core.util.ExcelUtils;

import com.jhsw.roadsun.framework.apilog.core.annotation.ApiAccessLog;
import static com.jhsw.roadsun.framework.apilog.core.enums.OperateTypeEnum.*;

import com.jhsw.roadsun.module.business.controller.admin.config.vo.*;
import com.jhsw.roadsun.module.business.dal.dataobject.config.BizConfigDO;
import com.jhsw.roadsun.module.business.service.config.BizConfigService;

@Tag(name = "管理后台 - 参数配置")
@RestController
@RequestMapping("/business/biz-config")
@Validated
public class BizConfigController {

    @Resource
    private BizConfigService bizConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建参数配置")
    @PreAuthorize("@ss.hasPermission('business:biz-config:create')")
    public CommonResult<Long> createBizConfig(@Valid @RequestBody BizConfigSaveReqVO createReqVO) {
        return success(bizConfigService.createBizConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新参数配置")
    @PreAuthorize("@ss.hasPermission('business:biz-config:update')")
    public CommonResult<Boolean> updateBizConfig(@Valid @RequestBody BizConfigSaveReqVO updateReqVO) {
        bizConfigService.updateBizConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除参数配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:biz-config:delete')")
    public CommonResult<Boolean> deleteBizConfig(@RequestParam("id") Long id) {
        bizConfigService.deleteBizConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除参数配置")
                @PreAuthorize("@ss.hasPermission('business:biz-config:delete')")
    public CommonResult<Boolean> deleteBizConfigList(@RequestParam("ids") List<Long> ids) {
        bizConfigService.deleteBizConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得参数配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:biz-config:query')")
    public CommonResult<BizConfigRespVO> getBizConfig(@RequestParam("id") Long id) {
        BizConfigDO bizConfig = bizConfigService.getBizConfig(id);
        return success(BeanUtils.toBean(bizConfig, BizConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得参数配置分页")
    @PreAuthorize("@ss.hasPermission('business:biz-config:query')")
    public CommonResult<PageResult<BizConfigRespVO>> getBizConfigPage(@Valid BizConfigPageReqVO pageReqVO) {
        PageResult<BizConfigDO> pageResult = bizConfigService.getBizConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BizConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出参数配置 Excel")
    @PreAuthorize("@ss.hasPermission('business:biz-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBizConfigExcel(@Valid BizConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BizConfigDO> list = bizConfigService.getBizConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "参数配置.xls", "数据", BizConfigRespVO.class,
                        BeanUtils.toBean(list, BizConfigRespVO.class));
    }

}